package com.zg.message.channel;

import com.zg.common.core.dao.message.entity.MsgInfo;
import com.zg.common.core.dao.message.entity.MsgTemplate;
import com.zg.message.service.MsgTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/7/9 14:45
 */

@Service
public class ChannelRouter {

    @Autowired
    // Spring 自动注入所有 Channel 实现
    private Map<String, IChannel> channels;

    @Autowired
    private MsgTemplateService msgTemplateService;
    public boolean routeAndSend(MsgInfo msgInfo) {
        Long templateId = msgInfo.getTemplateId();
        MsgTemplate msgTemplate = msgTemplateService.getById(templateId);
        IChannel channel = channels.get(msgTemplate.getType().toLowerCase() + "Channel");
        if (channel == null) {
            throw new IllegalArgumentException("不支持的通道：" + msgTemplate.getType());
        }
        return channel.send(msgInfo);
    }
}
